package com.demo.forum.services.impl;

import com.demo.forum.common.Result;
import com.demo.forum.common.ResultCode;
import com.demo.forum.dao.MessageMapper;
import com.demo.forum.dao.UserMapper;
import com.demo.forum.model.Message;
import com.demo.forum.model.User;
import com.demo.forum.model.exception.ApplicationException;
import com.demo.forum.services.IMessageService;
import com.demo.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j


public class MessageServiceImpl implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void create(Message message) {
        //1. 参数校验
        if(message == null || message.getPostUserId() == null||
                message.getReceiveUserId() == null || StringUtil.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.判断接收方是否存在
        User user = userMapper.selectByPrimaryKey(message.getReceiveUserId());
       if(user == null || user.getDeleteState() == 1) {
           log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
           throw new ApplicationException(Result.failed(ResultCode.FAILED_USER_NOT_EXISTS));
       }


        //构造更新对象
        message.setState((byte) 0);
       message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int row = messageMapper.insert(message);
        if(row != 1){
            log.warn(ResultCode.FAILED_CREATE.toString() +", 受影响行数不为1!");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CREATE));
        }


    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if(count == null ){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <=0 ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id == null || id <= 0 ||state <0 || state >2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        if(message == null || message.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        Message updateMessage = new Message();

        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
        }


    }

    @Override
    public Message selectById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null || repliedId <= 0 || message == null ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(Result.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
//        Message updateMessage = new Message();
//        updateMessage.setId(existsMessage.getId());
//        updateMessage.setState((byte)2);
//        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
//        if(row != 1){
//            log.warn(ResultCode.ERROR_SERVICES.toString());
//            throw new ApplicationException(Result.failed(ResultCode.ERROR_SERVICES));
//        }
        updateStateById(repliedId,(byte) 2);
        create(message);
    }
}
